﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Configuration;

public partial class AreaAnno : System.Web.UI.Page
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["group"] == null)
        {
            Response.Redirect("../Login.aspx");
        }
        else if ((int)Session["group"] != 2)
        {
            Response.Redirect("../Login.aspx");
        }
        if (Request.QueryString.ToString() == "")
        {
            Response.Redirect("AreaAnnoList.aspx");
        }
        string[] AnnoArray;
        string Annoid = "";
        int max = 0;
        id = Int16.Parse(Request.QueryString.ToString());
        SqlConnection sqlcon = null;
        SqlCommand sqlcmd = null;
        SqlCommand sqlcmd2 = null;
        SqlCommand sqlcmd3 = null;
        SqlDataReader read = null;
        string cmd = "";
        try
        {
            sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
            sqlcon.Open();
            cmd = "select title,[content],time from Announcement where id='" + id + "'";
            sqlcmd = new SqlCommand(cmd, sqlcon);
            read = sqlcmd.ExecuteReader();
            read.Read();
            AnnoTitle.Text = read.GetValue(0).ToString();
            AnnoText.Text = read.GetValue(1).ToString();
            AnnoTime.Text = read.GetValue(2).ToString();
            read.Close();
            cmd = "select Announcement from member where id='" + Session["uid"].ToString() + "'";
            sqlcmd2 = new SqlCommand(cmd, sqlcon);
            read = sqlcmd2.ExecuteReader();
            read.Read();
            AnnoArray = read.GetValue(0).ToString().Split(';');
            read.Close();
            max = AnnoArray.Length - 2;
            for (int i = 0; i <= max; i++)
            {
                if (Int16.Parse(AnnoArray[i]) == id)
                {
                    AnnoArray[i] = "-1";
                }
            }
            for (int i = 0; i <= max; i++)
            {
                if (AnnoArray[i] != "-1")
                {
                    Annoid = Annoid + AnnoArray[i]+";";
                }
            }
            cmd = "update member set Announcement='" + Annoid + "' where id='" + Session["uid"] + "'";
            sqlcmd3 = new SqlCommand(cmd, sqlcon);
            sqlcmd3.ExecuteNonQuery();
        }
        catch (SqlException sqle)
        {
            ;
        }
        finally
        {
            sqlcon.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AreaAnnoList.aspx");
    }
}